package com.opener.common.code.assistant.gen;

import org.springframework.util.StringUtils;

import java.util.SortedSet;
import java.util.TreeSet;

public class ServiceImplGenerator extends DaoGenerator{

    public ServiceImplGenerator(Ctx ctx, Ctx.MvcClass mvcClass) {
        super(ctx, mvcClass);
    }



    @Override
    public String createInsertFunc(){
        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        Ctx.MvcClass daoCls = getCtx().getMvcClass("dao");
        StringBuffer sb = new StringBuffer();
        sb.append("\n\t@Override\n");
        sb.append("\tpublic Integer add("+modelCls.getClassName()+" "+Utils.lowerFistAlpha(modelCls.getClassName())+"){\n");
        sb.append("\t\tInteger ret = "+Utils.lowerFistAlpha(daoCls.getClassName())+".add("+Utils.lowerFistAlpha(modelCls.getClassName())+");\n");
        sb.append("\t\treturn ret > 0 ? "+Utils.lowerFistAlpha(modelCls.getClassName())+".getId():null;\n");
        sb.append("\t}\n");
        return sb.toString();
    }
    public String createUpdateFunc(){
        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        Ctx.MvcClass daoCls = getCtx().getMvcClass("dao");
        StringBuffer sb = new StringBuffer();
        sb.append("\n\t@Override\n");
        sb.append("\tpublic boolean update("+modelCls.getClassName()+" "+Utils.lowerFistAlpha(modelCls.getClassName())+"){\n");
        sb.append("\t\tboolean ret = "+Utils.lowerFistAlpha(daoCls.getClassName())+".update("+Utils.lowerFistAlpha(modelCls.getClassName())+");\n");
        sb.append("\t\treturn ret;\n");
        sb.append("\t}\n");
        return sb.toString();
    }
    public String createGetFunc(){
        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        Ctx.MvcClass daoCls = getCtx().getMvcClass("dao");
        StringBuffer sb = new StringBuffer();
        sb.append("\n\t@Override\n");
        sb.append("\tpublic "+modelCls.getClassName()+" get(Integer id){\n");
        sb.append("\t\t"+modelCls.getClassName()+" ret = "+Utils.lowerFistAlpha(daoCls.getClassName())+".get(id);\n");
        sb.append("\t\treturn ret;\n");
        sb.append("\t}\n");
        return sb.toString();
    }
    public String createSearchFunc(){
        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        Ctx.MvcClass searchCls = getCtx().getMvcClass("search");
        Ctx.MvcClass daoCls = getCtx().getMvcClass("dao");
        StringBuffer sb = new StringBuffer();
        sb.append("\n\t@Override\n");
        sb.append("\tpublic List<"+modelCls.getClassName()+"> search("+searchCls.getClassName()+" args){\n");
        sb.append("\t\tList<"+modelCls.getClassName()+"> ret = "+Utils.lowerFistAlpha(daoCls.getClassName())+".search(args);\n");
        sb.append("\t\treturn ret;\n");
        sb.append("\t}\n");
        return sb.toString();
    }

    public String createSearchCountFunc(){
        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        Ctx.MvcClass searchCls = getCtx().getMvcClass("search");
        Ctx.MvcClass daoCls = getCtx().getMvcClass("dao");
        StringBuffer sb = new StringBuffer();
        sb.append("\n\t@Override\n");
        sb.append("\tpublic Integer getSearchCount("+searchCls.getClassName()+" args){\n");
        sb.append("\t\tInteger totalCount = "+Utils.lowerFistAlpha(daoCls.getClassName())+".getSearchCount(args);\n");
        sb.append("\t\treturn totalCount;\n");
        sb.append("\t}\n");
        return sb.toString();
    }
    @Override
    public String createJavaString(){
        Ctx.MvcClass cls = getMvcClass();

        Ctx.MvcClass modelCls = getCtx().getMvcClass("model");
        Ctx.MvcClass daoCls = getCtx().getMvcClass("dao");
        Ctx.MvcClass serviceCls = getCtx().getMvcClass("service");
        Ctx.MvcClass searchArgsCls = getCtx().getMvcClass("search");
        String pkgLine = "package "+cls.getPkg()+";\n\n";
        SortedSet importLines = new TreeSet();
        importLines.add("import java.util.List;");
        importLines.add("import "+ modelCls.getPkg()+"."+modelCls.getClassName()+";");
        importLines.add("import "+ searchArgsCls.getPkg()+"."+searchArgsCls.getClassName()+";");
        importLines.add("import "+ daoCls.getPkg()+"."+daoCls.getClassName()+";");
        importLines.add("import "+ serviceCls.getPkg()+"."+serviceCls.getClassName()+";");
        importLines.add("import org.springframework.stereotype.Service;");
        importLines.add("import org.springframework.beans.factory.annotation.Autowired;");
        StringBuffer classBody = new StringBuffer();
        classBody.append("@Service\n");
        classBody.append("public class "+cls.getClassName()+" implements "+serviceCls.getClassName()+"{\n");
        classBody.append("\t@Autowired\n");
        classBody.append("\tprivate "+daoCls.getClassName()+" "+Utils.lowerFistAlpha(daoCls.getClassName())+";\n\n");
        String insertFunc = createInsertFunc();
        String updateFunc = createUpdateFunc();
        String getFunc = createGetFunc();
        String searchFunc = createSearchFunc();
        String searchCountFunc = createSearchCountFunc();

        classBody.append(insertFunc);
        classBody.append(updateFunc);
        classBody.append(getFunc);
        classBody.append(searchFunc);
        classBody.append(searchCountFunc);

        classBody.append("}");
        String clsStr = pkgLine +
                StringUtils.collectionToDelimitedString(importLines,"\n") +"\n\n"+
                createClassDescString()+
                classBody.toString();
        return clsStr;
    }
}
